﻿using Library;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FANZHONGYU_STD_C.practice.Library
{
    public sealed class LibrarySystemNoLocalDataClient : LibrarySystemNoLocalData
    {
        public override void AddBookMenu()
        {
            if (!JudgeAdminConsole().Item1) return; // 管理员认证

            Console.Write("请输入书名: ");
            var title = Console.ReadLine();
            Console.Write("请输入作者: ");
            var author = Console.ReadLine();
            Console.Write("请输入数量: ");
            if (int.TryParse(Console.ReadLine(), out var quantity))
            {
                if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(author))
                {
                    Console.Write("书名或作者无效!");
                    return;
                }
                AddBook(title, author, quantity);
                Console.WriteLine($"成功添加书籍: 《{title}》");
            }
            else Console.WriteLine("数量无效，请输入一个有效整数!");
        }

        public override void DeleteBookMenu()
        {
            if (!JudgeAdminConsole().Item1) return; // 管理员认证

            Console.Write("请输入要删除的书名: ");
            var title = Console.ReadLine();
            if (string.IsNullOrEmpty(title))
            {
                Console.WriteLine("书名无效!");
                return;
            }
            DeleBook(title);
        }

        public override void BorrowBookMenu()
        {
            var (flag, user) = JudgeUserConsole();
            if (!flag) return;

            Console.Write("请输入书名: ");
            var title = Console.ReadLine();
            Console.Write("请输入借阅数量: ");
            if (int.TryParse(Console.ReadLine(), out var quantity))
            {
                if (string.IsNullOrEmpty(title))
                {
                    Console.WriteLine("书名无效!");
                    return;
                }
                var borrowFalg = BorrowBook(title, quantity);
                if (borrowFalg) UserBorrowOrReturnBook(OperatorConstants.BORROWBOOK, user, title, quantity);
            }
            else Console.WriteLine("数量无效，请输入一个有效整数!");
        }

        public override void ReturnBookMenu()
        {
            var (flag, user) = JudgeUserConsole();
            if (!flag) return;

            Console.Write("请输入书名: ");
            var title = Console.ReadLine();
            Console.Write("请输入归还数量: ");

            if (int.TryParse(Console.ReadLine(), out var quantity))
            {
                if (string.IsNullOrEmpty(title))
                {
                    Console.WriteLine("书名无效!");
                    return;
                }
                var returnFlag = UserBorrowOrReturnBook(OperatorConstants.BORROWBOOK, user, title, quantity);
                if (returnFlag) ReturnBook(title, quantity);
            }
            else Console.WriteLine("数量无效，请输入一个有效整数!");
        }

        public override void QueryBookMenu()
        {
            Console.WriteLine("查询选项:");
            Console.WriteLine("1. 按书名查询");
            Console.WriteLine("2. 按作者查询");
            Console.WriteLine("3. 按书名和作者查询");
            Console.Write("请选择查询方式 (1-3): ");
            var option = Console.ReadLine();

            switch (option)
            {
                case "1":
                    Console.Write("请输入书名关键字: ");
                    var title = Console.ReadLine();
                    QueryBook(title: title);
                    break;
                case "2":
                    Console.Write("请输入作者关键字: ");
                    var author = Console.ReadLine();
                    QueryBook(author: author);
                    break;
                case "3":
                    Console.Write("请输入书名关键字: ");
                    title = Console.ReadLine();
                    Console.Write("请输入作者关键字: ");
                    author = Console.ReadLine();
                    QueryBook(title: title, author: author);
                    break;
                default:
                    Console.WriteLine("无效的选项，请重新选择!");
                    break;
            }
        }

        public override void QueryBorrowedBookMenu()
        {
            var (flag, user) = JudgeUserConsole();
            if (flag) QueryBorrowedBook(user);
        }
    }
}
